package fr.iut.tp3.view;

import java.awt.Dimension;

import javax.swing.JFrame;

import fr.iut.tp3.Context;
import fr.iut.tp3.controller.events.Event;
import fr.iut.tp3.controller.events.EventDispatcher;
import fr.iut.tp3.controller.events.EventListener;
import fr.iut.tp3.view.components.MainMenuBar;
import fr.iut.tp3.view.components.MainPanel;

public class MainView extends JFrame implements EventListener {

	// Dimensions par défaut.
	public static final int DEFAULT_WIDTH = 950;
	public static final int DEFAULT_HEIGHT = 600;

	private static final long serialVersionUID = 8961285326288696106L;
	private MainPanel mainPanel;
	private MainMenuBar mainMenuBar;
	private Context context;

	public MainView(Context context){

		this.context = context;

		// Remplissage de la fenêtre.
		addMenu(context);
		addContent(context);

		// Paramétrage de la fenêtre.
		setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Enregistrement auprès du dispatcher d'événements.
		EventDispatcher.getInstance().registerListener(this);
	}

	private void addContent(Context context) {
		mainPanel = new MainPanel(context);
		setContentPane(mainPanel);
	}

	private void addMenu(Context context) {
		mainMenuBar = new MainMenuBar(context,this);
		setJMenuBar(mainMenuBar);
	}
	
	public void rendreVisible(){
		this.pack();
		this.setVisible(true);
	}

	@Override
	public void onEvent(Event event) {
		switch (event) {
		case LOGIN_SUCCESS:
			rendreVisible();
			break;			
		case REFRESH:
			this.revalidate();
			break;
		default:
			break;
		}
	}

	public MainPanel getMainPanel() {
		return mainPanel;
	}

	public MainMenuBar getMainMenuBar() {
		return mainMenuBar;
	}

	public Context getContext() {
		return context;
	}	
}
